package com.zz.framework.core.ext

import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.widget.ImageView
import androidx.annotation.NonNull
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.*
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.zz.framework.core.entity.SelfGlideUrl
import java.security.MessageDigest

/**
 * Glide加载图片
 * @param url 可以是网络，可以是File，可以是资源id等等Glide支持的类型
 * @param placeholder 默认占位图
 * @param error 失败占位图
 * @param isCircle 是否是圆形，默认false，注意：isCircle和roundRadius两个只能有一个生效
 * @param isCenterCrop 是否设置scaleType为CenterCrop，你也可以在布局文件中设置
 * @param roundRadius 圆角角度，默认为0，不带圆角，注意：isCircle和roundRadius两个只能有一个生效
 * @param isCrossFade 是否有过渡动画，默认没有过渡动画
 * @param isForceOriginalSize 是否强制使用原图，默认false
 */

fun ImageView.loadUrl(
    url: Any?, placeholder: Int = 0, error: Int = 0,
    isCircle: Boolean = false,
    isCenterCrop: Boolean = true,
    roundRadius: Int = 0,
    isCrossFade: Boolean = false,
    isForceOriginalSize: Boolean = false
) {
    val options = RequestOptions().placeholder(placeholder).error(error).apply {
        if (isCenterCrop && scaleType != ImageView.ScaleType.CENTER_CROP)
            scaleType = ImageView.ScaleType.CENTER_CROP
        if (isCircle) {
            if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                transforms(CenterCrop(), CircleCrop())
            } else {
                transform(CircleCrop())
            }
        } else if (roundRadius != 0) {
            if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                transforms(CenterCrop(), RoundedCorners(roundRadius))
            } else {
                transform(RoundedCorners(roundRadius))
            }
        }
        if (isForceOriginalSize) {
            override(Target.SIZE_ORIGINAL)
        }
    }
    Glide.with(context).load(url)
        .apply(options)
//        .transform(GrayscaleTransformation())
        .apply {
            if (isCrossFade) transition(DrawableTransitionOptions.withCrossFade())
        }
        .into(this)
}
fun ImageView.loadUrlWiteCacheKey(
    url: String?, cacheKey:String? = null,placeholder: Int = 0, error: Int = 0,
    isCircle: Boolean = false,
    isCenterCrop: Boolean = true,
    roundRadius: Int = 0,
    isCrossFade: Boolean = false,
    isForceOriginalSize: Boolean = false
) {
    val options = RequestOptions().placeholder(placeholder).error(error).apply {
        if (isCenterCrop && scaleType != ImageView.ScaleType.CENTER_CROP)
            scaleType = ImageView.ScaleType.CENTER_CROP
        if (isCircle) {
            if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                transforms(CenterCrop(), CircleCrop())
            } else {
                transform(CircleCrop())
            }
        } else if (roundRadius != 0) {
            if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                transforms(CenterCrop(), RoundedCorners(roundRadius))
            } else {
                transform(RoundedCorners(roundRadius))
            }
        }
        if (isForceOriginalSize) {
            override(Target.SIZE_ORIGINAL)
        }
    }
    Glide.with(context)
        .load(SelfGlideUrl(url,if (cacheKey.isNullOrBlank()) url else cacheKey))
        .load(url)
        .apply(options)
        .apply {
            if (isCrossFade) transition(DrawableTransitionOptions.withCrossFade())
        }
        .into(this)

}
fun ImageView.loadThumbnailUrl(
    url: Any?, placeholder: Int = 0, error: Int = 0,
    isCircle: Boolean = false,
    isCenterCrop: Boolean = true,
    roundRadius: Int = 0,
    isCrossFade: Boolean = false,
    isForceOriginalSize: Boolean = false
) {
    val options = RequestOptions().placeholder(placeholder).error(error).apply {
        if (isCenterCrop && scaleType != ImageView.ScaleType.CENTER_CROP)
            scaleType = ImageView.ScaleType.CENTER_CROP
        if (isCircle) {
            if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                transforms(CenterCrop(), CircleCrop())
            } else {
                transform(CircleCrop())
            }
        } else if (roundRadius != 0) {
            if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                transforms(CenterCrop(), RoundedCorners(roundRadius))
            } else {
                transform(RoundedCorners(roundRadius))
            }
        }
        if (isForceOriginalSize) {
            override(Target.SIZE_ORIGINAL)
        }
    }
    Glide.with(context).load(url)
        .thumbnail(0.1f)
        .apply(options)
        .apply {
            if (isCrossFade) transition(DrawableTransitionOptions.withCrossFade())
        }
        .into(this)
}

fun ImageView.loadUrlImage(url: Any?, placeholder: Int = 0, error: Int = 0) {
    val options = RequestOptions().placeholder(placeholder).error(error).apply {
        scaleType = ImageView.ScaleType.CENTER_CROP
    }
//    var url = "http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg"
    Glide.with(context).load(url)
        .apply(options)
        .into(this)
}


fun ImageView.loadVideoScreenshot(uri: String, error: Int = 0, frameTimeMicros: Long = 1) {
    val requestOptions = RequestOptions.frameOf(frameTimeMicros)
    requestOptions.set(VideoDecoder.FRAME_OPTION, MediaMetadataRetriever.OPTION_CLOSEST)
    requestOptions.transform(object : BitmapTransformation() {
        override fun transform(
            @NonNull pool: BitmapPool,
            @NonNull toTransform: Bitmap,
            outWidth: Int,
            outHeight: Int
        ): Bitmap {
            return toTransform
        }

        override fun updateDiskCacheKey(messageDigest: MessageDigest) {
            try {
                messageDigest.update(
                    (context.getPackageName()
                        .toString() + "RotateTransform").toByteArray(charset("utf-8"))
                )
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        }
    })
    Glide.with(context).load(uri).apply(requestOptions).also {
        if (error > 0) {
            it.placeholder(error).error(error)
        }
        it.into(this)
    }
}
